package pl.edu.agh.mikro.BluetoothVehicle;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;





public class TCPServer {
	
    public static int SERVERPORT = 8081;

    private ServerSocket serverSocket;
    private ServerThread server;
    private BluetoothChat chat;
    
    private final Handler mHandler;
   
    

    public TCPServer(BluetoothChat btc, Handler handler, int port) {
    	//Toast.makeText(chat, "Starting TCPServer", Toast.LENGTH_SHORT).show();
    	chat = btc;
        SERVERPORT = port;
        mHandler = handler;
        server = new ServerThread();
        server.start();
    }
    
    public synchronized void start() {
        // Start the thread to listen on a BluetoothServerSocket
        if (server == null) {
        	server = new ServerThread();
            server.start();
        }
    }
 
    private class ServerThread extends Thread {
 
        public void run() {
            try {
                if (chat != null) {
                 
                    serverSocket = new ServerSocket(SERVERPORT);
                    while (true) {
                    	
                    	
                    	/*Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_TOAST);
                        Bundle bundle = new Bundle();
                        bundle.putString(BluetoothChat.TOAST, "Waiting for connections");
                        msg.setData(bundle);
                        mHandler.sendMessage(msg);*/
                    	
                    	

                        Socket client = serverSocket.accept();


                        try {
                            InputStreamReader in = new InputStreamReader(client.getInputStream());
                            int c = -1;
                            while ((c = in.read()) != -1) {
                            	if(c==50 || c == 51 ||( c>96 && c<103)) {
                            		
                            		Message msg1 = mHandler.obtainMessage(BluetoothChat.MESSAGE_RECVMSG);
                                    Bundle bundle1 = new Bundle();
                                    bundle1.putString(BluetoothChat.RECVMSG, Character.toString((char)c));
                                    msg1.setData(bundle1);
                                    mHandler.sendMessage(msg1);
 
                            	}
                            }
                            client.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    public void onDestroy() {
    	if (serverSocket != null) {
	    	try {
	            serverSocket.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
    	}
    }
}

